from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from app.core.config import settings

# MySQL 特定配置
engine = create_engine(
    settings.DATABASE_URL,
    pool_pre_ping=True,  # 自动重连
    pool_recycle=3600,   # 连接池回收时间
    pool_size=5,         # 连接池大小
    max_overflow=10      # 最大溢出连接数
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

# Dependency
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close() 